In your final repo, there should be an R markdown file that organizes all computational steps for evaluating your proposed Facial Expression Recognition framework.
This file is currently a template for running evaluation experiments. You should update it according to your codes but following precisely the same structure.
set.seed(2020)
# setwd("~/GitHub/Fall2020-Project3-group1/doc")
# change the working directory as needed
# if someone can make this a relative path, that would be great!!!
Provide directories for training images. Training images and Training fiducial points will be in different subfolders.
# change the directory of the data to where it's stored in your local drive as needed
train_dir <- "../data/train_set/" # This will be modified for different data sets.
# train_dir <- "~/train_set/"
train_image_dir <- paste(train_dir, "images/", sep="")
train_pt_dir <- paste(train_dir, "points/", sep="")
train_label_path <- paste(train_dir, "label.csv", sep="")
In this chunk, we have a set of controls for the evaluation experiments.
K <- 5 # number of CV folds
run.fudicial.list <- FALSE
run.feature.train <- FALSE # process features for training set
run.feature.test <- FALSE # process features for test set
sample.reweight <- TRUE # run sample reweighting in model training
run.cv.gbm <- FALSE # run cross-validation on the training set for gbm
run.train.gbm <- FALSE # run evaluation on entire train set
run.test.gbm <- TRUE # run evaluation on an independent test set
run.cv.xgboost <- FALSE # run cross-validation on the training set for xgboost
run.train.xgboost <- FALSE # run evaluation on entire train set
run.test.xgboost <- TRUE # run evaluation on an independent test set
# add controls here to make if else statements to either cross-validate, test, train, or to just load saved data
# for xgboost, we need to also train and test each time we knit to record the time for the model
Using cross-validation or independent test set evaluation, we compare the performance of models with different specifications. In this Starter Code, we tune parameter lambda (the amount of shrinkage) for logistic regression with LASSO penalty.
# hyperparameters for our models
# gbm model (baseline)
hyper_grid_gbm <- expand.grid(
shrinkage = c(0.001, 0.005, 0.010, 0.050, 0.100),
n.trees = c(600, 1200, 1800)
)
# xgboost model
hyper_grid_xgboost <- expand.grid(
eta = c(0.01, 0.05, 0.1, 0.2, 0.3),
lambda = c(0.001, 0.005, 0.010, 0.050, 0.100),
gamma = c(0, 5),
nrounds = c(100, 200, 600)
)
# add more hyperparameters for each model as needed
#train-test split
info <- read.csv(train_label_path)
n <- nrow(info)
n_train <- round(n*(4/5), 0)
train_idx <- sample(info$Index, n_train, replace = F)
test_idx <- setdiff(info$Index, train_idx)
Fiducial points are stored in matlab format. In this step, we read them and store them in a list.
n_files <- length(list.files(train_image_dir))
if (run.fudicial.list){
#function to read fiducial points
#input: index
#output: matrix of fiducial points corresponding to the index
readMat.matrix <- function(index){
return(round(readMat(paste0(train_pt_dir, sprintf("%04d", index), ".mat"))[[1]],0))
}
#load fiducial points
fiducial_pt_list <- lapply(1:n_files, readMat.matrix)
save(fiducial_pt_list, file="../output/fiducial_pt_list.RData")
} else {
load(file="../output/fiducial_pt_list.RData")
}
The follow plots show how pairwise distance between fiducial points can work as feature for facial emotion recognition.
Figure1
feature.R should be the wrapper for all your feature engineering functions and options. The function feature( ) should have options that correspond to different scenarios for your project and produces an R object that contains features and responses that are required by all the models you are going to evaluate later.
feature.Rsource("../lib/feature.R")
tm_feature_train <- NA
if(run.feature.train){
tm_feature_train <- system.time(dat_train <- feature(fiducial_pt_list, train_idx))
save(dat_train, tm_feature_train, file="../output/feature_train.RData")
}else{
load(file="../output/feature_train.RData")
}
tm_feature_test <- NA
if(run.feature.test){
tm_feature_test <- system.time(dat_test <- feature(fiducial_pt_list, test_idx))
save(dat_test, tm_feature_test, file="../output/feature_test.RData")
}else{
load(file="../output/feature_test.RData")
}
Call the train model and test model from library.
train.R and test.R should be wrappers for all your model training steps and your classification/prediction steps.
train.R
test.R
source("../lib/train_gbm.R")
source("../lib/test_gbm.R")
source("../lib/cross_validation_gbm.R")
feature_train = as.matrix(dat_train[, -6007])
label_train = as.integer(dat_train$label)
if(run.cv.gbm){
res_cv <- matrix(0, nrow = nrow(hyper_grid_gbm), ncol = 4)
for(i in 1:nrow(hyper_grid_gbm)){
cat("n.trees = ", hyper_grid_gbm$n.trees[i], ",
shrinkage = ", hyper_grid_gbm$shrinkage[i],"\n", sep = "")
res_cv[i,] <- cv.function(features = feature_train, labels = label_train,
num_trees = hyper_grid_gbm$n.trees[i],
shrink = hyper_grid_gbm$shrinkage[i],
K, reweight = sample.reweight)
save(res_cv, file="../output/res_cv_gbm.RData")
}
}else{
load("../output/res_cv_gbm.RData")
}
*Visualize cross-validation results.
res_cv_gbm <- as.data.frame(res_cv)
colnames(res_cv_gbm) <- c("mean_error", "sd_error", "mean_AUC", "sd_AUC")
gbm_cv_results = data.frame(hyper_grid_gbm, res_cv_gbm)
# Mean Error
ggplot(gbm_cv_results, aes(as.factor(shrinkage), as.factor(n.trees), fill = mean_error)) +
geom_tile()
# Mean AUC
ggplot(gbm_cv_results, aes(as.factor(shrinkage), as.factor(n.trees), fill = mean_AUC)) +
geom_tile()
# Mean Error
# N.Trees = 600
ggplot(gbm_cv_results[gbm_cv_results$n.trees == 600, ],
aes(x = as.factor(shrinkage), y = mean_error,
ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# N.Trees = 1200
ggplot(gbm_cv_results[gbm_cv_results$n.trees == 1200, ],
aes(x = as.factor(shrinkage), y = mean_error,
ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# N.Trees = 1800
ggplot(gbm_cv_results[gbm_cv_results$n.trees == 1800, ],
aes(x = as.factor(shrinkage), y = mean_error,
ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# Mean AUC
# N.Trees = 600
ggplot(gbm_cv_results[gbm_cv_results$n.trees == 600, ],
aes(x = as.factor(shrinkage), y = mean_AUC,
ymin = mean_AUC - sd_AUC, ymax = mean_AUC + sd_AUC)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# N.Trees = 1200
ggplot(gbm_cv_results[gbm_cv_results$n.trees == 1200, ],
aes(x = as.factor(shrinkage), y = mean_AUC,
ymin = mean_AUC - sd_AUC, ymax = mean_AUC + sd_AUC)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# N.Trees = 1800
ggplot(gbm_cv_results[gbm_cv_results$n.trees == 1800, ],
aes(x = as.factor(shrinkage), y = mean_AUC,
ymin = mean_AUC - sd_AUC, ymax = mean_AUC + sd_AUC)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
par_best_gbm_ind <- which(gbm_cv_results$mean_AUC == max(gbm_cv_results$mean_AUC))
par_best_gbm_shrinkage <- gbm_cv_results$shrinkage[par_best_gbm_ind]
par_best_gbm_n.trees <- gbm_cv_results$n.trees[par_best_gbm_ind]
if (run.train.gbm) {
# training weights
weight_train <- rep(NA, length(label_train))
for (v in unique(label_train)){
weight_train[label_train == v] = 0.5 * length(label_train) / length(label_train[label_train == v])
}
if (sample.reweight){
tm_train_gbm <- system.time(fit_train_gbm <- train(feature_train, label_train, w = weight_train,
num_trees = par_best_gbm_n.trees,
shrink = par_best_gbm_shrinkage))
} else {
tm_train_gbm <- system.time(fit_train_gbm <- train(feature_train, label_train, w = NULL,
num_trees = par_best_gbm_n.trees,
shrink = par_best_gbm_shrinkage))
}
save(fit_train_gbm, tm_train_gbm, file="../output/fit_train_gbm.RData")
} else {
load(file="../output/fit_train_gbm.RData")
}
label_test
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[60] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[119] 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[178] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[237] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[296] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[355] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[414] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[473] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[532] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[591] 1 1 1 1 1 1 1 1 1 1
## reweight the test data to represent a balanced label distribution
weight_test <- rep(NA, length(label_test))
for (v in unique(label_test)){
weight_test[label_test == v] = 0.5 * length(label_test) / length(label_test[label_test == v])
}
# convert the original 1-2 class into numeric 0s and 1s
label_test <- ifelse(label_test == 2, 0, 1)
accu <- sum(weight_test * (label_pred == label_test)) / sum(weight_test)
tpr.fpr <- WeightedROC(prob_pred, label_test, weight_test)
auc <- WeightedAUC(tpr.fpr)
The accuracy of the gbm model (shinkage = 0.05, n.trees = 1200) is 74.4%.
The AUC of the gbm model (shinkage = 0.05, n.trees = 1200) is 0.8086568.
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.
Time for constructing training features = 2.14 seconds
Time for constructing testing features = 0.16 seconds
Time for training gbm model = 233.97 seconds
Time for testing gbm model = 20.623 seconds
Call the train model and test model from library.
train.R and test.R should be wrappers for all your model training steps and your classification/prediction steps.
train.R
test.R
source("../lib/cross_validation_xgboost.R")
source("../lib/train_xgboost.R")
source("../lib/test_xgboost.R")
if(run.cv.xgboost){
res_cv <- matrix(0, nrow = nrow(hyper_grid_xgboost), ncol = 4)
for (i in 1:nrow(hyper_grid_xgboost)){
print(i)
res_cv[i,] <- cv.function(features = feature_train, labels = label_train,
K,
eta_val = hyper_grid_xgboost$eta[i],
lmd = hyper_grid_xgboost$lambda[i],
gam = hyper_grid_xgboost$gamma[i],
nr = hyper_grid_xgboost$nrounds[i])
save(res_cv, file="../output/res_cv_xgboost.RData")
}
}else{
load("../output/res_cv_xgboost.RData")
}
*Visualize cross-validation results.
res_cv_xgboost <- as.data.frame(res_cv)
colnames(res_cv_xgboost) <- c("mean_error", "sd_error", "mean_AUC", "sd_AUC")
res_cv_xgboost_cv_results = data.frame(hyper_grid_xgboost, res_cv_xgboost)
# Mean Error
ggplot(res_cv_xgboost_cv_results, aes(as.factor(nrounds), as.factor(eta), fill = mean_error)) +
geom_tile()
# Mean AUC
ggplot(res_cv_xgboost_cv_results, aes(as.factor(nrounds), as.factor(eta), fill = mean_AUC)) +
geom_tile()
# Mean Error
# nrounds = 100
ggplot(res_cv_xgboost_cv_results[res_cv_xgboost_cv_results$nrounds == 100, ],
aes(x = as.factor(eta), y = mean_error,
ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# nrounds = 200
ggplot(res_cv_xgboost_cv_results[res_cv_xgboost_cv_results$nrounds == 200, ],
aes(x = as.factor(eta), y = mean_error,
ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# nrounds = 600
ggplot(res_cv_xgboost_cv_results[res_cv_xgboost_cv_results$nrounds == 600, ],
aes(x = as.factor(eta), y = mean_error,
ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# Mean AUC
# N.Trees = 600
ggplot(res_cv_xgboost_cv_results[res_cv_xgboost_cv_results$nrounds == 100, ],
aes(x = as.factor(eta), y = mean_AUC,
ymin = mean_AUC - sd_AUC, ymax = mean_AUC + sd_AUC)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# N.Trees = 1200
ggplot(res_cv_xgboost_cv_results[res_cv_xgboost_cv_results$nrounds == 200, ],
aes(x = as.factor(eta), y = mean_AUC,
ymin = mean_AUC - sd_AUC, ymax = mean_AUC + sd_AUC)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
# N.Trees = 1800
ggplot(res_cv_xgboost_cv_results[res_cv_xgboost_cv_results$nrounds == 600, ],
aes(x = as.factor(eta), y = mean_AUC,
ymin = mean_AUC - sd_AUC, ymax = mean_AUC + sd_AUC)) +
geom_crossbar() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
par_best_res_cv_xgboost_cv_results_ind <- which(
res_cv_xgboost_cv_results$mean_AUC == max(res_cv_xgboost_cv_results$mean_AUC))
par_best_res_cv_xgboost_cv_results_eta <- res_cv_xgboost_cv_results$eta[par_best_res_cv_xgboost_cv_results_ind]
par_best_res_cv_xgboost_cv_results_lambda <- res_cv_xgboost_cv_results$lambda[par_best_res_cv_xgboost_cv_results_ind]
par_best_res_cv_xgboost_cv_results_gamma <- res_cv_xgboost_cv_results$gamma[par_best_res_cv_xgboost_cv_results_ind]
par_best_res_cv_xgboost_cv_results_nrounds <- res_cv_xgboost_cv_results$nrounds[par_best_res_cv_xgboost_cv_results_ind]
if (run.train.xgboost) {
# training weights
weight_train <- rep(NA, length(label_train))
for (v in unique(label_train)){
weight_train[label_train == v] = 0.5 * length(label_train) / length(label_train[label_train == v])
}
label_train_binary = ifelse(label_train == 2, 0, 1)
train <- as.data.frame(cbind(feature_train, label_train_binary))
param <- list(objective = "binary:logistic", eval_metric = "auc")
if (sample.reweight){
tm_train_xgboost <- system.time(fit_train_xgboost <- xgboost(data = feature_train,
label = label_train_binary,
weight = weight_train,
params = param,
eta = par_best_res_cv_xgboost_cv_results_eta,
lambda = par_best_res_cv_xgboost_cv_results_lambda,
gamma = par_best_res_cv_xgboost_cv_results_gamma,
nrounds = par_best_res_cv_xgboost_cv_results_nrounds,
verbose = 0,
max_depth = 1))
} else {
label_train_binary = ifelse(label_train == 2, 0, 1)
train <- as.data.frame(cbind(feature_train, label_train_binary))
tm_train_xgboost <- system.time(fit_train_xgboost <- xgboost(data = feature_train,
label = label_train_binary,
weight = NULL,
params = param,
eta = par_best_res_cv_xgboost_cv_results_eta,
lambda = par_best_res_cv_xgboost_cv_results_lambda,
gamma = par_best_res_cv_xgboost_cv_results_gamma,
nrounds = par_best_res_cv_xgboost_cv_results_nrounds,
verbose = 0,
max_depth = 1))
}
save(fit_train_xgboost, tm_train_gbm, file="../output/fit_train_xgboost.RData")
} else {
load(file="../output/fit_train_xgboost.RData")
}
tm_test_xgboost= NA
if(T){
load(file="../output/fit_train_xgboost.RData")
tm_test_xgboost <- system.time({prob_pred <- predict(fit_train_xgboost, feature_test);
label_pred <- ifelse(prob_pred >= 0.5, 1, 0)})
}
## reweight the test data to represent a balanced label distribution
weight_test <- rep(NA, length(label_test))
for (v in unique(label_test)){
weight_test[label_test == v] = 0.5 * length(label_test) / length(label_test[label_test == v])
}
feature_test <- as.matrix(dat_test[, -6007])
label_test <- as.integer(dat_test$label)
# convert the original 1-2 class into numeric 0s and 1s
label_test <- ifelse(label_test == 2, 0, 1)
accu <- sum(weight_test * (label_pred == label_test)) / sum(weight_test)
tpr.fpr <- WeightedROC(prob_pred, label_test, weight_test)
auc <- WeightedAUC(tpr.fpr)
[1] 0.7870653
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.
Time for constructing training features = 2.14 seconds
Time for constructing testing features = 0.16 seconds
Time for training xgboost model = 138.835 seconds
Time for testing xgboost model = 0.112 seconds
Call the train model and test model from library.
train.R and test.R should be wrappers for all your model training steps and your classification/prediction steps.
train.R
test.R
*Visualize cross-validation results.
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.
Call the train model and test model from library.
train.R and test.R should be wrappers for all your model training steps and your classification/prediction steps.
train.R
test.R
*Visualize cross-validation results.
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.
Call the train model and test model from library.
train.R and test.R should be wrappers for all your model training steps and your classification/prediction steps.
train.R
test.R
*Visualize cross-validation results.
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.